| 创建时间: | 2015/8/17 19:28 |
| 来源: | http://blog.csdn.net/yufaw/article/details/8471484 |
今天老师讲了jsp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用!
1、form表单
2、request.setAttribute();和request.getAttribute();
3、超链接:<a herf="index.jsp"?a=a&b=b&c=c>name</a>
4、<jsp:param>
下面一一举例说明:
1、form表单
form.jsp:
<%@page contentTypetext/html; charset=GB2312%>
title
form.jsp file
</title
</
style="background-color:lightblue"
style="font-family:arial;color:red;font-size:25px;text-align:center"登录页面</
action="result.jsp" method="get" align="center"
姓名:input ="text"="name"="20" value maxlength="20"></>
密码:input ="password"="password"="20" value maxlength="20"></>
<!--在爱好前空一个空格,是为了排版好看些-->
爱好:input ="checkbox"="hobby" value="唱歌"唱歌
input ="checkbox"="hobby" value="足球"足球
input ="checkbox"="hobby" value="篮球"篮球/></>
input ="submit"="submit" value="登录"
input ="reset"="reset" value="重置"></>
</
</
</
result.jsp:
<%@page language importjava.util.* pageEncodingGB2312%>
title
result.jsp file
</title
</
bgcolor="ffffff"
<%
request.setCharacterEncoding(GB2312Stringrequest.getParameter();
name String(name.getBytes(iso-8859-1GB2312Stringrequest.getParameter(passwordString[] hobbyrequest.getParameterValues(hobby注意这里的函数是getParameterValues()接受一个数组的数据
%>
<%(!name.equals( !pwd.equals(%>
您好!登录成功!/>
姓名:<%%>/>
密码:<%%>/>
爱好:<%String ho: hobby)
{
ho String(ho.getBytes(iso-8859-1GB2312);
out.print(ho %>
<%%>
请输入姓名或密码!
<%%>
</
</
注意:form表单的提交方式为get,在参数传递时会遇到中文乱码的问题,一个简单的解决方法是,将接受到的字符串先转换成一个byte数组,再用String构造一个新的编码格式的String,如:
String name=request.getParameter("name");
name=new String(name.getBytes("iso-8859-1"),"GB2312");
如果form表单的提交方式为post,解决乱码问题的简单办法是,使用 request.setCharacterEncoding("GB2312");设置request的编码方式。
为什么会出现中文乱码问题呢?因为Tomcat服务器默认的系统编码方式为iso-8859-1,你传递参数给服务器时,使用的是默认的iso-8859-1的编码方式,但是服务器向你返回信息时,是按page指令中设置的编码方式,如:<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>,这样就混合了两种编码方式,所以会出现乱码,所以解决之道就是统一传递和接收的编码方式。
2、request.setAttribute()和request.getAttribute()
set.jsp:
<%@page contentTypetext/html; charset=GB2312%>
title
set.jsp file
</title
</
style="background-color:lightblue"
<%
request.setAttribute(%>
jsp:forward ="get.jsp"/>
</
</
get.jsp:
<%@page contentTypetext/html; charset=GB2312%>
title
get.jsp file
</title
</
style="background-color:lightblue"
<%
out.println(传递过来的参数是:request.getAttribute(%>
</
</
request.setAttribute()和request.getAttribute()是配合<jsp:forward>或是include指令来实现的。
3、超链接:<a herf="index.jsp?a=a&b=b&c=c">name</a>
href.jsp:
<%@page contentTypetext/html; charset=GB2312%>
title
href.jsp file
</title
</
style="background-color:lightblue"
="getHerf.jsp?name=心雨&password=123"传递参数</
</
</
getHref.jsp:
<%@page contentTypetext/html; charset=GB2312%>
title
getHref.jsp file
</title
</
style="background-color:lightblue"
<%Stringrequest.getParameter();
name String(name.getBytes(iso-8859-1gb2312);
out.print(name:name);
%>
/>
<%
out.print(password:request.getParameter(password%>
</
</
这种传递参数的方法和form表单的get方式类似,是通过地址栏传递的参数,其乱码解决方法也和form 的get方式一样。
4、<jsp:param>
param.jsp:
<%@page contentTypetext/html; charset=GB2312%>
title
param.jsp file
</title
</
style="background-color:lightblue"
<%request.setCharacterEncoding(GB2312%>
jsp:forward ="getParam.jsp"
jsp:param ="name" value="心雨"/>
jsp:param ="password" value="123"/>
</jsp:forward
</
</
getParam.jsp:
<%@page contentTypetext/html; charset=GB2312%>
title
getParam.jsp file
</title
</
style="background-color:lightblue"
<%Stringrequest.getParameter();
out.print(name:name);
%>
/>
<%
out.print(password:request.getParameter(password%>
</
</
这里发现了一个奇怪的问题,还是在中文乱码的问题上,在form表单的例子中,如果传递方式为post,则只需要在接收参数的页面设置request的编码方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收参数的页面,如果将该句放到form表单里,那么不起作用,仍然是乱码。而在本例中,为了使传递的参数不出现乱码,却是将request.setCharacterEncoding("GB2312");放在发送参数的页面中,才会正常显示中文,放在接收参数的页面中,不起作用。也许这就是<jsp:param>和form表单传递参数不同的地方。为什么会有这个不同呢?可能是因为form表单中的参数是由客户端传送到服务端上的,需要经过一个request的打包过程,但是<jsp:param>传递的参数本身就是在服务器端的,不需要经历由客户端到服务端这么一个过程,但是服务器里的参数传递是这么回事呢?这个问题,我不知道了!真是知识是一个扩大的圆圈,你知道的越多,那么不知道的就越多!努力吧!